package com.abkj.platform.tail.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.abkj.platform.tail.service.ISMSService;
import com.abkj.platform.tail.service.SerialService;
import com.abkj.platform.tail.util.DateUtil;
import com.abkj.platform.tail.util.JsonUtils;

@Controller
@RequestMapping({ "read" })
public class ReadDataController extends BaseController {

	@Autowired
	SerialService serialService;
	@Autowired
	private ISMSService smsService;

	@RequestMapping(value = { "data" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String readData(String table) {
		return this.serialService.scan(table);
	}

	@RequestMapping(value = { "setRainTime" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String setRainTime(HttpServletRequest request, String code) {
		createJcUserLog(request, "设置", "雨量计时间设置");
		return String.valueOf(this.serialService.setRainTime(code));
	}

	@RequestMapping(value = { "getRainTime" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String getRainTime(HttpServletRequest request, String code) {
		createJcUserLog(request, "设置", "雨量计时间读取");
		return this.serialService.getRainTime(code);
	}

	@RequestMapping(value = { "alarm" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String alarm() {
		//smsService.sendMsg(null, null, null);
		List alarms = new ArrayList();
		List codes = new ArrayList();
		Map ret = new HashMap();
		/* 1、渗压计 */
		List<Map> maps = this.baseService.findBySql("select * from osmometer");
		int info;
		for (Map map : maps) {
			map.put("typename", "渗压计");
			info = NumberUtils.toInt(String.valueOf(map.get("info")));
			String code = String.valueOf(map.get("code"));
			if (info != 0) {
				alarms.add(map);
			} else {
				float depth = NumberUtils.toFloat(String.valueOf(map.get("depth")));
				float alarm = NumberUtils.toFloat(String.valueOf(map.get("alarm")));
				float offer = NumberUtils.toFloat(String.valueOf(map.get("offer")));
				float tmp = offer * 100.0F / 9.8F;
				if (depth - tmp <= alarm){
					map.put("alertInfo", "内部浸润水面超过报警深度");
					alarms.add(map);
				} else {
					codes.add(code);
				}
			}
		}
		/* 2、静力水准仪 */
		List<Map> maps2 = this.baseService.findBySql("select * from level");
		int info2;
		for (Map map : maps2) {
			map.put("typename", "静力水准仪");
			info2 = NumberUtils.toInt(String.valueOf(map.get("info")));
			String code = String.valueOf(map.get("code"));
			if (info2 != 0) {
				alarms.add(map);
			} else {
				float alertup = NumberUtils.toFloat(String.valueOf(map.get("alertup")));
				float alertdown = NumberUtils.toFloat(String.valueOf(map.get("alertdown")));
				float val = NumberUtils.toFloat(String.valueOf(map.get("val")));
				if (val > alertup) {
					map.put("alertInfo", "测量值高于上升报警参数");
					alarms.add(map);
				}else if (val < alertdown){
					map.put("alertInfo", "测量值低于下降报警参数");
					alarms.add(map);
				} else {
					codes.add(code);
				}
			}
		}
		/* 3、超声波液位仪 */
		List<Map> maps3 = this.baseService.findBySql("select * from ultrasonic");
		for (Map map : maps3) {
			map.put("typename", "超声波液位仪");
			String code = String.valueOf(map.get("code"));
			float level1 = NumberUtils.toFloat(String.valueOf(map.get("level1")));
			float level2 = NumberUtils.toFloat(String.valueOf(map.get("level2")));
			float val = NumberUtils.toFloat(String.valueOf(map.get("val")));
			if (val > level2){
				map.put("alertInfo", "二级报警");
				alarms.add(map);
			} else if (val > level1){
				map.put("alertInfo", "一级报警");
				alarms.add(map);
			} else {
				codes.add(code);
			}
			/*float height = NumberUtils.toFloat(String.valueOf(map.get("height")));
			float lenh = NumberUtils.toFloat(String.valueOf(map.get("lenh")));
			float should = NumberUtils.toFloat(String.valueOf(map.get("should")));
			if (height - should >= lenh){
				map.put("alertInfo", "测量值低于下降报警参数");
				alarms.add(map);
			} else {
				codes.add(code);
			}*/
		}
		/* 4、雨量计 */
		List<Map> maps4 = this.baseService.findBySql("select * from ultrasonic");
		for (Map map : maps4) {
			map.put("typename", "雨量计");
			String code = String.valueOf(map.get("code"));
			int alertval = NumberUtils.toInt(String.valueOf(map.get("alertval")));
			
			Calendar instance = Calendar.getInstance();
			Date time = instance.getTime();
			String date = DateUtil.dateToStr(time, DateUtil.DATE_FORMAT_YYYY_MM_DD2);
			List rains = this.baseService.findBySql("select * from raindata where day=" + date);
			float allday = 0.0F;
			float max = 0.0F;
			float min = 0.0F;
			List<Float> xs = new ArrayList();
			if (rains.size() > 0) {
				Map row = (Map) rains.get(0);
				String allVal = String.valueOf(map.get("allval"));
				allday = NumberUtils.toFloat(String.valueOf(map.get("val")));
				int len = allVal.length() / 4;
				for (int i = 0; i < 24; i++){
					if (i < len) {
						String tmp = allVal.substring(i * 4, i * 4 + 4);
						xs.add(Float.valueOf(Integer.parseInt(tmp, 16) * 0.1F));
					} else {
						xs.add(null);
					}
				}
			} else {
				for (int i = 0; i < 24; i++)
					xs.add(null);
			}
			for (Float x : xs) {
				if (x != null) {
					if (x.floatValue() > max) {
						max = x.floatValue();
					}
					if (x.floatValue() < min)
						min = x.floatValue();
				}
			}
			if (max > alertval){
				map.put("alertInfo", "最大降雨量预警");
				alarms.add(map);
			} else {
				codes.add(code);
			}
		}
		/* 5、位移计 */
		List<Map> maps5 = this.baseService.findBySql("select * from displacement");
		int info5;
		for (Map map : maps5) {
			map.put("typename", "位移计");
			info5 = NumberUtils.toInt(String.valueOf(map.get("info")));
			String code = String.valueOf(map.get("code"));
			if (info5 != 0) {
				alarms.add(map);
			} else {
				float alertup = NumberUtils.toFloat(String.valueOf(map.get("alertup")));
				float alertdown = NumberUtils.toFloat(String.valueOf(map.get("alertdown")));
				float val = NumberUtils.toFloat(String.valueOf(map.get("val")));
				if (val > alertup) {
					map.put("alertInfo", "测量值高于上升报警参数");
					alarms.add(map);
				}else if (val < alertdown){
					map.put("alertInfo", "测量值低于下降报警参数");
					alarms.add(map);
				} else {
					codes.add(code);
				}
			}
		}
		ret.put("alarms", alarms);
		ret.put("normal", codes);
		if (alarms.size() > 0) {
			Map<String, Object> form = smsService.getForm();
			Object ableObj = form.get("able");
			Object autoObj = form.get("auto");
			if (ableObj != null &&autoObj != null && (boolean)ableObj) {
				if ((boolean)autoObj) {//自动发送模式
					ret.put("autoAlert", true);
					//发送预警短信
					smsService.sendMsg(null, null, null);
				}else {
					ret.put("autoAlert", false);
				}
			}
		}
		return JsonUtils.writeObjToString(ret);
	}
}